<template>
  <div class="totallist">
    <div class="dustbin">
      <div class="all" :class="{ active: msg == 0 }" @click="msg = 0">
        全部<span>({{ amount }})</span>
      </div>
      <div class="always" :class="{ active: msg == 1 }" @click="msg = 1">
        常买<span>(0)</span>
      </div>
      <div class="lowprice" :class="{ active: msg == 2 }" @click="msg = 2">
        降价<span>(0)</span>
      </div>
      <div class="manage" @click="changeFn">
        <span v-if="isHid">编辑</span>
        <span v-if="!isHid">取消</span>
      </div>
    </div>
    <!-- 商品列表 -->
    <div class="goodlist">
      <ul>
        <li v-for="(item, index) in goodList" :key="item.id">
          <a href="#">
            <div class="detail_good">
              <div class="good_radius">
                <input type="checkbox" v-model="item.check" />
                <i class="icon" @click="thenClidk(index)"></i>
              </div>
              <div class="good_img">
                <img :src="item.path" alt="" />
              </div>
              <div class="detail_msg">
                <div class="good_txt">
                  <span>{{ item.text }}</span>
                </div>
                <div class="good_price">
                  <div class="unit_price">
                    <span>￥{{ item.price }}</span>
                  </div>
                  <div class="p_num">
                    <a
                      href="javascript:void(0);"
                      class="decrement"
                      @click="decFn(index)"
                      >-</a
                    >
                    <input type="text" class="itxt" v-model="item.num" />
                    <a
                      href="javascript:void(0);"
                      class="increment"
                      @click="subFn(index)"
                      >+</a
                    >
                  </div>
                </div>
              </div>
            </div>
          </a>
        </li>
      </ul>
    </div>
    <!-- 商品总计 -->
    <div class="item_total" v-if="isTotal">
      <div class="good_total">
        <span>总计:</span>
        <span>￥{{ totalPrice }}</span>
      </div>
    </div>
    <!-- 自己下单 -->
    <div class="selectAll">
      <div class="place_all" @click="allSelect">
        <input type="checkbox" v-model="check" />
        <i class="icon"></i>
      </div>
      <div class="order">
        <span>全选</span>
      </div>
      <div class="total_price">
        <span>￥{{ totalPrice }}</span>
      </div>
      <!-- 立即下单 -->
      <div class="order_immed" v-if="isHid" @click="orderFn">
        <span>立即下单</span>
      </div>
      <!-- 收藏与删除 -->
      <div class="p_order" v-if="!isHid">
        <div class="collect">
          <span>收藏</span>
        </div>
        <div class="delete" @click="deleteFn">
          <span>删除</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodList: [
        // {
        //   path: "http://47.115.51.185//uploads/attach/2021/06/20210626/0265a3cb5dd2492e3fc8a4293361bf8e.jpg",
        //   text: "中国风棉麻t恤男装中式短袖麻料盘扣中袖上衣大码亚麻T恤夏季宽松",
        //   price: "30.6",
        //   id: 1,
        //   num: 1,
        //   check: false,
        // },
        // {
        //   path: "http://47.115.51.185//uploads/attach/2021/06/20210626/0265a3cb5dd2492e3fc8a4293361bf8e.jpg",
        //   text: "中国风棉麻t恤男装中式短袖麻料盘扣中袖上衣大码亚麻T恤夏季宽松",
        //   price: "20.6",
        //   id: 2,
        //   num: 1,
        //   check: false,
        // },
      ],
      isTotal: true,
      isHid: true,
      amount: 0,
      msg: 0,
      num: 1,
      check: false,
    };
  },
  computed: {
    totalPrice: function () {
      let total = 0;
      this.goodList.forEach(function (item) {
        if (item.check) {
          total += item.num * item.price;
        }
      });
      total = total.toFixed(2);
      return total;
    },
  },
  methods: {
    //开关灯
    changeFn() {
      this.isHid = !this.isHid;
    },
    //单击
    thenClidk(index) {
      let selected = this.goodList[index];
      if (selected.check == true) {
        selected.check = false;
      } else {
        selected.check = true;
      }
    },
    //全选
    allSelect() {
      let _this = this;
      this.check = !this.check;
      if (this.check) {
        _this.goodList.forEach((item) => {
          item.check = true;
        });
      } else if (!this.check) {
        _this.goodList.forEach((item) => {
          item.check = false;
        });
      }
    },
    // 自减
    decFn(index) {
      let num = this.goodList[index].num;
      if (num < 2) {
        num = 1;
      } else {
        num--;
        this.goodList[index].num = num;
      }
    },
    // 自增
    subFn(index) {
      let list = this.goodList;
      let count = list[index].num;
      count = count + 1;
      list[index].num = count;
    },
    // 删除
    deleteFn() {
      // console.log(this.goodList);
      this.goodList = this.goodList.filter(function (item) {
        return !item.check;
      });
    },
    orderFn(){
      console.log(111);
      this.$toast("下单失败");
    },
    init() {
      let token = sessionStorage.getItem("token");
      // console.log(token);
      axios({
        method: "GET",
        url: `http://47.115.51.185/api/cart/list`,
        headers: {
          "Authori-zation": "Bearer " + token,
        },
      }).then((res) => {
        let data = res.data.data.valid;
        data.forEach((item) => {
          let obj = {};
          obj.path = item.productInfo.image;
          obj.price = item.productInfo.price;
          obj.id = item.productInfo.id;
          obj.text =item.productInfo.store_name;
          obj.num = item.cart_num;
          obj.check = false;
          // console.log(obj,111);
          this.goodList.push(obj);
          // console.log(this.goodList,9231);
        });
      });
    },
  },
  watch: {
    goodList: {
      handler(value) {
        //  数组里的每个对象
        //  console.log(value.length);
        let _this = this;
        let count = 0;
        //  循环数组里的对象
        for (let i = 0; i < value.length; i++) {
          //  当check为true时,自增一
          if (value[i].check === true) {
            count++;
          }
        }
        if (count === value.length) {
          _this.check = true;
        } else {
          _this.check = false;
        }
      },
      deep: true,
    },
  },
  created() {
    this.init();
    this.amount = this.goodList.length;
  },
  updated() {
    if (this.goodList.length == 0) {
      this.isTotal = false;
      this.check = false;
    }
  },
};
</script>

<style lang="less">
@import "../assets/css/reset.css";
@import "../assets/less/variable.less";

.goodlist {
  width: 100%;
  ul li {
    margin-top: 10px;
  }
}
.detail_good {
  width: 345px;
  height: 100px;
  background: #fff;
  //   border: 1px solid #ccc;
  display: flex;
  font-size: 14px;
  padding-right: 15px;
  margin-left: 8px;
  border-radius: 3px;
}
//选中商品
.good_radius {
  width: 50px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  position: relative;
  input {
    display: none;
  }
  .icon {
    position: absolute;
    display: inline-block;
    width: 18px;
    height: 18px;
    border: 1px solid #ccc;
    border-radius: 50%;
    top: 31%;
    left: 30%;
  }
  input:checked + .icon {
    background-color: #cf3bed;
    border-color: #cf3bed;
    background-image: url();
    background-size: 0.21rem 0.15rem;
    background-repeat: no-repeat;
    background-position: 50%;
  }
}
//商品图片
.good_img {
  width: 80px;
  height: 80px;
  margin-left: 10px;
  margin-right: 10px;
  margin-top: 10px;
  img {
    width: 100%;
    height: 100%;
    display: block;
  }
}
//商品信息
.detail_msg {
  width: 210px;
  height: 100px;
  .good_txt {
    height: 50px;
    line-height: 50px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 16px;
  }
  .good_price {
    width: 100%;
    height: 50px;
    &::before {
      content: "";
      display: block;
      clear: both;
    }
    div {
      float: left;
    }
    .unit_price {
      width: 100px;
      height: 50px;
      line-height: 50px;
      font-size: 16px;
    }
    .p_num {
      width: 110px;
      height: 25px;
      border: 1px solid #ccc;
      position: absolute;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-left: 100px;
      margin-top: 12px;
      text-align: center;
      a {
        width: 30px;
        height: 25px;
        line-height: 25px;
        display: block;
        font-size: 18px;
        color: #999;
      }
      input {
        width: 40px;
        height: 23px;
        outline: none;
        text-align: center;
        font-size: 16px;
        border: 1px solid #ccc;
      }
    }
  }
}

//商品总计
.item_total {
  width: 360px;
  height: 50px;
  margin-left: 8px;
  background: #fff;
  border-top: 2px solid #f1f1f1;
  border-bottom-left-radius: 15px;
  border-bottom-right-radius: 15px;
  .good_total {
    height: 50px;
    line-height: 50px;
    font-size: 18px;
    float: right;
    padding-right: 15px;
    span {
      &:last-child {
        font-size: 24px;
        color: #cf3bed;
      }
    }
  }
}
//自己下单
.selectAll {
  width: 100%;
  height: 60px;
  display: flex;
  background: #fff;
  position: absolute;
  bottom: 50px;
  .place_all {
    width: 50px;
    height: 60px;
    text-align: center;
    line-height: 100px;
    position: relative;
    input {
      display: none;
    }
    .icon {
      position: absolute;
      display: inline-block;
      width: 18px;
      height: 18px;
      border: 1px solid #ccc;
      border-radius: 50%;
      top: 31%;
      left: 30%;
    }
    input:checked + .icon {
      background-color: #cf3bed;
      border-color: #cf3bed;
      background-image: url();
      background-size: 0.21rem 0.15rem;
      background-repeat: no-repeat;
      background-position: 50%;
    }
  }
  .order {
    width: 50px;
    height: 60px;
    line-height: 60px;
    font-size: 18px;
  }
  .total_price {
    width: 100px;
    height: 60px;
    line-height: 60px;
    text-align: left;
    color: #cf3bed;
    font-size: 18px;
    overflow: hidden;
  }
  .order_immed {
    width: 150px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    border-radius: 20px;
    color: #fff;
    background: linear-gradient(to right, #7028e4, #ee609c);
    margin-top: 12px;
    font-size: 16px;
    margin-left: 18px;
  }
  .p_order {
    width: 180px;
    height: 60px;
    display: flex;
    .collect {
      width: 80px;
      height: 30px;
      line-height: 30px;
      font-size: 16px;
      border-radius: 20px;
      text-align: center;
      margin-top: 15px;
      color: #9f9f9f;
      border: 1px solid #9f9f9f;
    }
    .delete {
      width: 80px;
      height: 30px;
      line-height: 30px;
      font-size: 16px;
      border-radius: 20px;
      text-align: center;
      margin-top: 15px;
      margin-left: 5px;
      color: #cf3bed;
      border: 1px solid #cf3bed;
    }
  }
}

.dustbin {
  height: 50px;
  background: #fff;
  > div {
    float: left;
    width: 60px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    font-size: 18px;
    margin-left: 15px;
  }
  .always {
    margin-left: 25px;
  }
  .lowprice {
    margin-left: 25px;
  }
  .manage {
    width: 60px;
    height: 25px;
    line-height: 28px;
    border: 1px solid #ccc;
    font-size: 16px;
    margin-top: 10px;
    border-radius: 3px;
    margin-left: 50px;
  }
  .active {
    color: #aa04d2;
    border-bottom: 3px solid #aa04d2;
  }
}
</style>